﻿#region license
//------------------------------------------------------------------------------
// <copyright company="Ascertaint.org">
//     Copyright (c) Ascertaint.org.  All rights reserved.
// </copyright> 
//
// Authors: Ming Tsai (ming@ascertaint.org)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//------------------------------------------------------------------------------
#endregion

using System;
using System.Text.RegularExpressions;
using System.Web;
using Ums.CellPhoneInfo;

namespace Ums.CellPhoneInfo {
    /// <summary>
    /// 
    /// </summary>
    /// <remarks>
    /// USA and canada cellphone parser. format is (xxx) xxxx-xxxx; length is 10 not including country code.
    /// </remarks>
    public class USACellPhoneParser : CellPhoneParser {
        public static string PhonePattern = "^1";

        protected override bool IsValid(string num) {
            CellPhoneNumber o = ParseExact(num);
            int test = o.AreaCode.Length + o.Prefix.Length + o.PhoneNumber.Length;
            if (test != 10)
                return false;
            else
                return true;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="numToParse"></param>
        /// <returns></returns>
        protected override CellPhoneNumber ParseExact(string numToParse) {
            //decode and strip off non numeric first
            //
            string num = HttpUtility.UrlDecode(numToParse);
            num = StriptOffNonNumeric(num);

            CellPhoneNumber o = new CellPhoneNumber();
            o.CountryIsoCode = "us";
            o.CountryCode = "1";

            //strip off intl call prefix if any
            //
            string result = Regex.Replace(num, "^1", "");

            if (result.Length==10) {
                o._isValidNumber = true;
                o.AreaCode = result.Substring(0, 3);
                o.Prefix = result.Substring(3, 3);
                o.PhoneNumber = result.Substring(6, result.Length - 6);
            }
            else {
                throw new Exception("phone prefix not found - USA cellphone number must have 10 digits.");
            }

            return o;
        }
    }
}
